package com.fili.gui.components.graph;

import com.fili.core.params.DryParams;
import com.fili.core.storage.DataItem;
import com.fili.core.storage.DataItemType;

import javax.swing.*;
import java.awt.*;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Oct 21, 2006
 * Time: 10:00:06 PM
 */
public class DataGraph extends JComponent
{
    private float gridWidth;
    private float lineWidth;
    private DataItem[] items;
    private float scaleX;
    private float scaleY;
    private Rectangle drawArea;

    private int zeroW;
    private int zeroH;


    public DataGraph()
    {
        gridWidth = 1;
        lineWidth = 2;
        drawArea = new Rectangle();
    }

    private int scaleX(float x)
    {
        return (int) (x * scaleX) + drawArea.x;
    }

    private int scaleY(float y)
    {
        return drawArea.height - (int) (y * scaleY) + drawArea.y;
    }

    protected void paintComponent(Graphics g)
    {
        zeroH = g.getFontMetrics().getHeight();
        zeroW = g.getFontMetrics().stringWidth("0");
        drawArea.x = 60;
        drawArea.y = 20;
        drawArea.width = getWidth() - 150;
        drawArea.height = getHeight() - 60;

        scaleX = drawArea.width / 100.0f;
        scaleY = drawArea.height / 100.0f;

        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.white);
        g2d.fillRect(0, 0, getWidth(), getHeight());

        g2d.setStroke(new BasicStroke(gridWidth));
        g2d.setColor(Color.black);

        for (int i = 0; i <= 10; i++)
        {
            int px = scaleX(0);
            int py = scaleY(i * 10);

            g2d.drawLine(px, py, scaleX(100), py);
            String s = "" + (i * 10);
            int w = g.getFontMetrics().stringWidth(s);
            g2d.drawString(s, scaleX(0) - w - zeroW, scaleY(i * 10) + zeroH / 2);
        }

        DataItemType.Index[] val = DataItemType.Index.values();
        
        for (int i=0;i<val.length;i++)
        {
            g.setColor(DryParams.getParams().getDataGraphConfig().getColor(val[i]));
            g.fillRect(drawArea.x + drawArea.width + zeroW, drawArea.y + i * zeroH*3/2, zeroH, zeroH);
            g.setColor(Color.black);
            g.drawRect(drawArea.x + drawArea.width + zeroW, drawArea.y + i * zeroH*3/2, zeroH, zeroH);
        }

    }
}
